#include <gdk/gdkkeysyms.h>
#include <stdarg.h>

#include "sms.h"

	static gboolean
on_delete_event (GtkWidget *window,
		GdkEvent  *event,
		gpointer   unused_data)
{	
	gtk_main_quit();
	printf("Leaving sms...\n");
}

/**************************hjq add for poweroff window**********************************/
void create_poweroff_window()
{
	gint result;
	GtkWidget *poweroff_dlg;
	poweroff_dlg =  gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_NONE, ("Power down the System ?"));
	gtk_dialog_add_buttons(GTK_DIALOG(poweroff_dlg), ("OK"), RESPONSE_POWEROFF,
			("Cancel"), RESPONSE_QUIT, NULL);
	result = gtk_dialog_run(GTK_DIALOG(poweroff_dlg));
	gtk_widget_destroy(poweroff_dlg);

	switch (result)
	{
		case RESPONSE_POWEROFF:
			system("/sbin/poweroff");
			break;
		case RESPONSE_QUIT:
			break;
		default:
			printf("select default");
			break;
	}
}

	static gboolean
on_window_key_press (GtkWidget   *widget,
		GdkEventKey *event,
		gpointer     user_data)
{
	SMS *sms =user_data;
	int i;

	if(sms->busy){
		return FALSE;
	}

	printf("key->keyval=%d\n", event->keyval);
	if (event->keyval == POWEROFF_KEY)
		create_poweroff_window();
	
	if ((event->keyval == GDK_Right) || (event->keyval == GDK_Left)){
		printf("righe!\n");
		//sms->busy = TRUE ;
		printf("sms->busy=%d\n" ,sms->busy);
	}


	for(i=0;i<LAYOUT_WIN_COUNT;i++)
	{
		if(!sms->wins[i])
			break;
		if(sms->wins[i]->active && sms->wins[i]->key_press_func)
		{
			if(sms->wins[i]->key_press_func(sms->wins[i],event))
				break;			
		}
	}

	return FALSE;
}


static gboolean sms_exec_app_finish(Exec *exec,gpointer data)
{
	SMS *sms=data;
	exec_destroy(sms->app);
	sms->app=NULL;
	//g_object_set(test_layout->text,"text","Exit gqview...",NULL);
}

static void test2_exec_create(Layout *layout)
{
	printf("~~~~~~~ %s\n",__FUNCTION__);
	char *argv[]={"gqview","-f","/usr/share/pixmaps",NULL};
	SMS *sms=layout->sms;
#if 0
	sms->app=exec_create("gqview",3,argv,FALSE,
			test2_exec_finish,test_layout);
	if(!test_layout->exec)
		g_object_set(test_layout->text,"text","Load gqview error",NULL);
	else
		g_object_set(test_layout->text,"text","Loading gqview...",NULL);
#endif
}

////////////////////////////////////////////////////////////////
GdkPixbuf *sms_load_pic(const char *path,int width, int height)
{
	GdkPixbuf *pixbuf;
	GError* err = NULL; 
	gboolean ratio=FALSE;

	if(width==-1||height==-1)
		ratio=TRUE;

	pixbuf=gdk_pixbuf_new_from_file_at_scale(path,width,height,ratio,&err);

	if(!pixbuf)
		fprintf(stderr,"%s:%s\n",__FUNCTION__,err->message);
	return pixbuf;
}


int sms_get_xml_int(xmlNode *a_node,xmlChar* key,int def_val)
{
	int ret;
	xmlChar* val;
	val = xmlGetProp (a_node,key);
	if(!val)
		ret=def_val;
	else
		ret=atoi(val);
	return ret;
}

double sms_get_xml_double(xmlNode *a_node, xmlChar* key, double def_val)
{
	double ret;
	xmlChar* val;
	val = xmlGetProp (a_node,key);
	if(!val)
		ret=def_val;
	else
		ret=atof(val);
	return ret;
}

gboolean sms_get_xml_boolean(xmlNode *a_node, xmlChar* key)
{
	gboolean ret;
	xmlChar* val;
	val = xmlGetProp (a_node, key);
	if(!strcmp (val, "true"))
		ret = TRUE;
	else
		ret = FALSE;
	return ret;
}

gboolean sms_exec_app(SMS* sms,const char *cmd,int argc,char **argv)
{

}

int sms_get_win_id(SMS *sms,const char *name)
{
	int i ;
	for(i=LAYOUT_WIN_MAIN;i<LAYOUT_WIN_COUNT;i++)
	{
		if(!sms->wins[i])
			return -1;

		if(strcmp(name,sms->wins[i]->name)==0)
			return i;
	}
	return -1;
}

int sms_get_item_id(SMS *sms,const char *name)
{
	int i ;
	for(i=LAYOUT_ITEM_PROMPTBAR; i<LAYOUT_ITEM_COUNT; i++)
	{
		if(strcmp(name,sms->items[i]->name)==0)
			return i;
	}
	return -1;
}


/***********stjq add*******************/
void show_entry(Entry *entry)
{
  printf("~~~%s\n" ,__FUNCTION__);

  gtk_widget_show(entry->entry_lan);

  entry = entry->next ;
  gtk_widget_show(entry->entry_lan);

  entry = entry->next ;
  gtk_widget_show(entry->entry_lan);

  entry = entry->next ;
  gtk_widget_show(entry->entry_lan);

  entry = entry->next ;
  gtk_widget_show(entry->entry_lan);
}

  /***********stjq add*******************/
static gint save_lan_config()
{
		printf("~~~%s\n" ,__FUNCTION__);
    FILE *fp = NULL;

    // open download url file
    remove(NETWORK_CONFIG);
    if ((fp = fopen(NETWORK_CONFIG, "w+")) == NULL)
    {
        printf("cannot open file: %s!\n", NETWORK_CONFIG);
        return -1;
    }
    fputs("eth0", fp);

    fclose(fp);
    return 0;
}


  /***********stjq add*******************/
static gint save_wifi_config()
{
		printf("~~~%s\n" ,__FUNCTION__);
    FILE *fp = NULL;

    // open download url file
    remove(NETWORK_CONFIG);
    if ((fp = fopen(NETWORK_CONFIG, "w+")) == NULL)
    {
        printf("cannot open file: %s!\n", NETWORK_CONFIG);
        return -1;
    }
    fputs("NONE", fp);
		printf("~~~%s\n" ,__FUNCTION__);
    fclose(fp);
    return 0;
}

Layout *sms_change_win_with_property(SMS *sms, ...)
{
	va_list arg_ptr;
	const char *s;
	LAYOUT_WIN_ID win_id;

	va_start(arg_ptr, sms);
	win_id = va_arg(arg_ptr, LAYOUT_WIN_ID);

	if(win_id>=LAYOUT_WIN_COUNT)
		return NULL;
	if(sms->cur_win==win_id)
		return NULL;

	if(sms->items[LAYOUT_ITEM_PROMPTBAR]) {
		layout_destroy(sms->items[LAYOUT_ITEM_PROMPTBAR]);
	}

	layout_hide(sms->wins[sms->cur_win]);

	while(s=va_arg(arg_ptr, const char*))
	{
		if(strcmp(s, "xml_path") == 0) 
		{
		    s =  va_arg(arg_ptr, const char *);
		    layout_set_propertie(sms->wins[win_id], "xml_path", s);
	  } 
		else
	    	break;
	}
	va_end(arg_ptr);

	layout_show(sms->wins[win_id]);
	sms->cur_win=win_id;

	return sms->wins[win_id];
}

Layout *sms_change_win_to(SMS *sms, LAYOUT_WIN_ID win_id)
{
	if(win_id>=LAYOUT_WIN_COUNT)
		return NULL;
	if(sms->cur_win==win_id)
		return NULL;

	if(sms->items[LAYOUT_ITEM_PROMPTBAR]) {
		layout_destroy(sms->items[LAYOUT_ITEM_PROMPTBAR]);
	}

	/***********stjq add*******************/
	if((win_id == LAYOUT_WIN_LAN_SETTING) && (lan_is_show == LAN_LAYOUT_SHOW))
	{
	    Layout *layout = sms->wins[win_id] ;
	    LanLayout *lanlayout = layout->private ;
	    Entry *entry = lanlayout->entrys ;
	    save_lan_config();
	    show_entry(entry) ;
	}
	printf("wifi_is_show %d\n" ,wifi_is_show);
	if((win_id == LAYOUT_WIN_WIFI_SETTING) && (wifi_is_show == WIFI_LAYOUT_SHOW))
	{
		Layout *layout = sms->wins[win_id] ;
		WifiLayout *wifilayout = layout->private ;
		save_wifi_config();
		gtk_widget_show_all(wifilayout->list);
		g_object_set(wifilayout->bg_wifi->pic2,"visibility",GOO_CANVAS_ITEM_VISIBLE,NULL);
	}

	layout_hide(sms->wins[sms->cur_win]);

	layout_show(sms->wins[win_id]);
	sms->cur_win=win_id;

	return sms->wins[win_id];
}

int main(int argc, char *argv[])
{
	printf("~~~%s\n" ,__FUNCTION__);
	SMS *sms;
	//GtkWidget *window,*canvas;
	GooCanvasItem *root;

	gtk_set_locale ();
	gtk_init (&argc, &argv);
	//init_global();
	sms = (SMS *)g_malloc0(sizeof(SMS)+1);
	sms->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size (GTK_WINDOW (sms->window), 800, 480);
	gtk_window_set_position(GTK_WINDOW(sms->window) ,GTK_WIN_POS_CENTER);
	gtk_widget_show (sms->window);
	g_signal_connect (sms->window, "delete-event", G_CALLBACK (on_delete_event),
			NULL);

	sms->canvas = goo_canvas_new ();
	gtk_widget_show (sms->canvas);
	gtk_container_add (GTK_CONTAINER (sms->window), sms->canvas);

	root = goo_canvas_get_root_item (GOO_CANVAS (sms->canvas));


	//sms->deamon = update_deamon_new(sms);
	sms->theme = theme_new("default");
	if(!sms->theme)
	{
	fprintf(stderr,"sms load theme error.\n");
	return -1;
	}
	printf("~~~%s\n" ,__FUNCTION__);
	sms->wins[0] = layout_register_new_by_win_id(root, LAYOUT_WIN_MAIN, sms);
	sms->wins[1] = layout_register_new_by_win_id(root,LAYOUT_WIN_SETTING,sms);
	/***********stjq add*******************/
	sms->wins[2] = layout_register_new_by_win_id(root ,LAYOUT_WIN_DIANDU ,sms);
	sms->wins[3] = layout_register_new_by_win_id(root ,LAYOUT_WIN_LAN_SETTING ,sms);
	sms->wins[4] = layout_register_new_by_win_id(root ,LAYOUT_WIN_WIFI_SETTING,sms);

	sms->wins[5] = layout_register_new_by_win_id(root,LAYOUT_WIN_TEACHER ,sms);
	sms->wins[6] = layout_register_new_by_win_id(root,LAYOUT_WIN_TEACHER_SUBJECT ,sms);
	/***********stjq add*******************/
	sms->wins[7] = layout_register_new_by_win_id(root, LAYOUT_WIN_INTRODUCTION, sms);
	sms->wins[8] = layout_register_new_by_win_id(root, LAYOUT_WIN_TEST, sms);
	sms->wins[9] = layout_register_new_by_win_id(root, LAYOUT_WIN_VIDEO_LIST, sms);
  sms->wins[10] = layout_register_new_by_win_id(root,LAYOUT_WIN_TOP_LIST, sms);
	//sms->wins[2] = layout_register_new_by_win_id(root, LAYOUT_WIN_TEACHER_STYLE, sms);
	//sms->wins[2] = layout_register_new_by_win_id(root ,LAYOUT_WIN_DIANDU ,sms);
	//sms->wins[2] = layout_register_new_by_win_id(root,LAYOUT_WIN_TEST2,sms);	
	//sms->wins[3] = layout_register_new_by_win_id(root,LAYOUT_WIN_MEDIALIB,sms);	

	layout_show(sms->wins[0]);
	printf("~~~%s\n" ,__FUNCTION__);
	sms->items[0] = layout_register_new_by_item_id(root, LAYOUT_ITEM_PROMPTBAR, sms);
	layout_show(sms->items[0]);

	g_signal_connect (sms->window, "key-press-event",
			G_CALLBACK (on_window_key_press), sms);

	gtk_main ();

	return 0;
}
